﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Common.DBOper
{
    public class Cls_SocketSet
    {

        /// <summary>
        /// 更新Scoket配置
        /// </summary>
        /// <param name="testName">测试名称</param>
        /// <param name="testName">数据库路径</param>
        public int UpdateSocketSet(string IpAddress,string Port,int ChannelCount)
        {
            int i = 0;
            string connString = string.Format(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0} ;Persist Security Info=False;Jet OLEDB:Database Password={1}", Mdl_Class.FilePath, Mdl_Class.AccessPassWord);
            using (MyDb_Access mydb = new MyDb_Access(connString))
            {
                try
                {
                    mydb.Open();
                    string sql = string.Format("update T_SocketSet set IPAddress='{0}',Port='{1}',ChannelCount='{2}' where ID=1", IpAddress, Port, ChannelCount);
                    i = mydb.ExecuteNonQuery(sql);
                    mydb.Close();
                }
                catch (Exception ex)
                {
                    Mdl_Class.WriteLog.dbErrLog("UpdateSocketSet(string IpAddress,string Port)", ex.ToString());
                }
            }
            return i;
        }



        public Cls_Scoket GetSocket()
        {
            Cls_Scoket socketset = null;
            string connString = string.Format(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0} ;Persist Security Info=False;Jet OLEDB:Database Password={1}", Mdl_Class.FilePath, Mdl_Class.AccessPassWord);
            using (MyDb_Access mydb = new MyDb_Access(connString))
            {
                try
                {
                    mydb.Open();
                    string sql = "select * from T_SocketSet where ID=1";
                    mydb.ExecuteReader(sql);
                    if (mydb.DataReader.Read())
                    {
                        socketset = new Cls_Scoket();

                        socketset.Port = Convert.ToInt32(mydb.DataReader["Port"].ToString());
                        socketset.IpAddress = Convert.ToString(mydb.DataReader["IPAddress"].ToString());
                        socketset.ChannelCount = Convert.ToInt32(mydb.DataReader["ChannelCount"].ToString());

                    }

                    mydb.Close();
                }
                catch (Exception ex)
                {
                    socketset = null;
                    Mdl_Class.WriteLog.dbErrLog("Cls_Scoket GetSocket()", ex.ToString());
                }

                return socketset;
            }
        }

    }

    public class Cls_Scoket 
    {
        public string IpAddress;
        public int Port;
        public int ChannelCount;
    }
}
